Prev Next |
SaveItem stores changes to an item to the physical storage. The method returns True if the item was successfully saved, False otherwise.
bool SaveItem(
ItemDefinition item,
ItemChanges changes,
CallContext context
)
Sitecore calls the SaveItem method when an item has been changed and should be saved. The item parameter contains the definition item as it was last read from the physical storage. As the ItemDefinition class is immutable, the changes made to the item are specified in the changes parameter.
The ItemChanges class contains methods to detect which parts of an item have been modified. The example code below provides an example of how this class is used. Notice that, at times, a field value must be physically removed from the data store. This is indicated by the FieldChange.RemoveField property. Although a full discussion as to how this can happen is beyond the scope of this document, the reason has to do with default field values on Templates.
public override bool SaveItem(ItemDefinition item, ItemChanges changes,
CallContext context) {
if (changes.HasPropertiesChanged) {
string itemName =
changes.GetPropertyValue("name") as string;
if (itemName != null) {
WriteName(itemName);
}
ID templateID =
changes.GetPropertyValue("templateid") as ID;
if (templateID != null) {
WriteTemplateID(templateID);
}
ID masterID =
changes.GetPropertyValue("masterid") as ID;
if (masterID != null) {
WriteMasterID(masterID);
}
}
if (changes.HasFieldsChanged) {
Hashtable fieldChanges = changes.FieldChanges;
lock(fieldChanges.SyncRoot) {
foreach(ItemChanges.FieldChange change in
fieldChanges.Values) {
if (change.RemoveField) {
RemoveField(change.FieldID, change.Language,
change.Version);
else {
SaveField(change.FieldID, change.Language,
change.Version, change.Value);
}
}
Prev Next